//
//  BACommentPhotoCollectionView.m
//  YunFu
//
//  Created by Bager on 2018/9/7.
//  Copyright © 2018年 yunfu. All rights reserved.
//

#import "BACommentPhotoCollectionView.h"

#import "BACommentPhotoCell.h"
#import "ZLPhotoActionSheet.h"

@implementation BACommentPhotoCollectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每个item的大小，
    flowLayout.itemSize = CGSizeMake((kScreenWidth - kPublicMargin * 2 - 65) / 4.0, (kScreenWidth - kPublicMargin * 2 - 65) / 4.0);
    // 设置列的最小间距
    flowLayout.minimumInteritemSpacing = 5;
    // 设置最小行间距
    flowLayout.minimumLineSpacing = 5;
    // 设置布局的内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    // 滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    
    self.backgroundColor = [UIColor whiteColor];
    
    if (self)
    {
        self.dataSource = self;
        
        self.delegate = self;
        
        [self registerClass:[BACommentPhotoCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    }
    
    return self;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imgArray.count;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    BACommentPhotoCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
        
    [cell.img sd_setImageWithURL:[NSURL URLWithString:self.imgArray[indexPath.item]]];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    
    NSMutableArray *arrNetImages = [NSMutableArray array];
    for (NSString *urlStr in self.imgArray)
    {
        [arrNetImages addObject:@{@"ZLPreviewPhotoObj":[NSURL URLWithString:urlStr], @"ZLPreviewPhotoTyp": @(2)}];
    }
    
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    actionSheet.configuration.navBarColor = [UIColor blackColor];
    actionSheet.sender = nav;
    
    [actionSheet previewPhotos:arrNetImages index:indexPath.item hideToolBar:YES complete:^(NSArray * _Nonnull photos) {
        NSLog(@"%@", photos);
    }];
}
@end
